package com.example.mypplication.sortlist.newsortlist

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mypplication.R
import com.example.mypplication.sortlist.CharacterParser
import java.util.Locale
import java.util.Random

class NewSortListActivity : AppCompatActivity() {
    private lateinit var sourceDateList: List<SortModel>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_sort_list_new)

        initView()

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initView() {
        // 在Activity中使用
        val contactListView = findViewById<ContactListView>(R.id.contact_recycler_view)

        sourceDateList = filledData(resources.getStringArray(R.array.date)) // 填充数据
// 设置数据
        contactListView.setContacts(sourceDateList)

// 设置点击监听
        contactListView.onItemClickListener = { contact ->
            Toast.makeText(this, "点击了: ${contact.name}", Toast.LENGTH_SHORT).show()
        }

// 设置群发按钮监听
        contactListView.onGroupSendClickListener = {
            // 处理群发逻辑
        }
    }

    private fun filledData(date: Array<String>): List<SortModel> {
        return date.map {
            SortModel().apply {
                name = it
                sex = Random().nextInt(2)
                val pinyin = CharacterParser.getInstance().getSelling(it)
                val sortString = pinyin.substring(0, 1).uppercase(Locale.getDefault())
                sortLetters = if (sortString.matches("[A-Z]".toRegex())) sortString else "#"
            }
        }
    }
}